package com.freemalll.merchant.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.ConcurrentReferenceHashMap;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
public class BeanUtils {


    private static final Set<Class<?>> unknownEditorTypes =
            Collections.newSetFromMap(new ConcurrentReferenceHashMap<Class<?>, Boolean>(64));


    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static String[] getNullAndEmptyStrPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null || "".equals(srcValue)) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 拷贝时忽略空属性
     *
     * @param src
     * @param target
     */
    public static void copyPropertiesIgnoreNull(Object src, Object target) {
        if (src != null&&target != null) {
            org.springframework.beans.BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
        }

    }

    /**
     * 拷贝

     */
    public static <S, T> T convert(S source, Class<T> targetClass) {
        if (source == null) {
            return null;
        }
        T target = null;
        try {
            target = targetClass.getDeclaredConstructor().newInstance();
            BeanUtils.copyPropertiesIgnoreNull(source, target);
        } catch (Exception e) {
            log.error("对象转换失败", e);
        }
        return target;
    }

    /**
     * 拷贝时忽略空属性和空串
     *
     * @param src
     * @param target
     */
    public static void copyPropertiesIgnoreNullAndEmptyStr(Object src, Object target) {
        org.springframework.beans.BeanUtils.copyProperties(src, target, getNullAndEmptyStrPropertyNames(src));
    }

    /**
     * list bean 转换为另外一个list bean
     *
     * @param objs
     * @param dto
     * @param <T>
     * @return
     */
    public static <T> List<T> beanToBeanList(List objs, Class<T> dto) {
        if (objs == null) {
            return null;
        }
        List<T> list = JSON.parseArray(JSON.toJSONString(objs), dto);
        return list;
    }
}
